var updatePwd=function () {

    bootbox.setDefaults({
        'locale':'zh_CN'
    });

    return{
        init:function () {
            $('#updatePwdForm').bootstrapValidator({
                message: '该值无效',
                trigger: 'blur keyup',
                feedbackIcons: {
                    valid: 'glyphicon glyphicon-ok',
                    invalid: 'glyphicon glyphicon-remove',
                    validating: 'glyphicon glyphicon-refresh'
                },
                fields: {
                    /*验证：规则*/
                    oldPassword: {
                        validators: {
                            notEmpty: {
                                message: '旧密码不能为空'
                            },
                            stringLength: {
                                min: 6,
                                max: 19,
                                message: '旧密码长度大于5小于20'
                            },
                            regexp: {
                                regexp: /^[^ ]+$/,
                                message: '旧密码不能有空格'
                            }
                        }
                    },
                    newPassword: {
                        validators: {
                            notEmpty: {
                                message: '新密码不能为空'
                            },
                            identical: {
                                field: 'comfirmPassword',
                                message: '新密码与确认密码不一致！'
                            },
                            stringLength: {
                                min: 6,
                                max: 19,
                                message: '新密码长度大于5小于20'
                            },
                            regexp: {
                                regexp: /^[^ ]+$/,
                                message: '新密码不能有空格'
                            }

                        }
                    },
                    comfirmPassword: {
                        validators: {
                            identical: {
                                field: 'newPassword',
                                message: '新密码与确认密码不一致！'
                            },
                            notEmpty: {
                                message: '确认密码不能为空'
                            },
                            stringLength: {
                                min: 6,
                                max: 19,
                                message: '确认密码长度大于5小于20'
                            },

                            regexp: {
                                regexp: /^[^ ]+$/,
                                message: '确认密码不能有空格'
                            }
                        }
                    }
                }
            }).on('error.field.bv', function (e, data) {//‘用户确认密码’ 没输入的时候，‘用户新密码’不提示‘用户新密码与确认密码不一致’
                if (data.field == 'newPassword') {
                    var len1 = data.element.val().length;
                    var len2 = $('#comfirmPassword').val().length;
                    var k = data.element.val().indexOf(" ");
                    if (len1 > 5 && len2 < 6 && k < 0) {
                        var $parent = data.element.parents('.form-group');
                        $parent.removeClass('has-error');
                        $parent.find('.form-control-feedback[data-bv-icon-for="' + data.field + '"]').hide();
                        data.element.siblings('[data-bv-validator="identical"]').hide();
                    }
                }
            });
        },
        updatePwd:function(){
            var bootstrapValidator = $("#updatePwdForm").data('bootstrapValidator');
            bootstrapValidator.validate();
            if(bootstrapValidator.isValid()){
                $.ajax({
                    url: "/api/user/updatePwd",
                    data:{oldPassword:$('#oldPassword').val(),newPassword:$('#newPassword').val()},
                    type:'POST',
                    dataType: "json",
                    success: function (data) {
                        if(data.code =='401'){
                            bootbox.alert(data.message);
                        }else if(data.code =='200' && data.data){
                           bootbox.alert(data.message);
                        }
                    }
                });
            }
        },
        formReset:function(){
            $('#updatePwdForm')[0].reset();
            $('#id').val('');
            $(':input','#updatePwdForm').removeAttr('checked');

            $("#updatePwdForm").data('bootstrapValidator').destroy();
            $("#updatePwdForm").data('bootstrapValidator',null);
        }
    }
}();